﻿using System;
using ExplorerService.Common.ServiceContracts;
using ExplorerService.Managers;

namespace ExplorerService.Communication
{
    public class ClientSessionService : IClientSessionService, IDisposable
    {
        /// <summary>
        /// Stores the current session ID
        /// </summary>
        private Guid _sessionId = Guid.Empty;

        /// <summary>
        /// Starts a new session
        /// </summary>
        /// <returns>The session ID</returns>
        public Guid GetSessionToken()
        {
            _sessionId = ClientSessionManager.Instance.StartNewClientSession();
            return _sessionId;
        }

        /// <summary>
        /// Receives pings from clients (validates that service is alive)
        /// </summary>
        /// <returns>True</returns>
        public bool Ping()
        {
            return true;
        }

        /// <summary>
        /// Ends the current session
        /// </summary>
        public void EndSession()
        {

        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            if (_sessionId != Guid.Empty)
            {
                ClientSessionManager.Instance.EndSession(_sessionId);
            }
        }
    }
}